package com.CTAtracker.android.Fragments;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Activities.SeveralStationActivity;
import com.CTAtracker.android.Activities.StationActivity;
import com.CTAtracker.android.Adapters.SeveralStationAdapter;
import com.CTAtracker.android.DAO.StationDataSource;
import com.CTAtracker.android.Models.Station;

public class SeveralStationFragment extends Fragment {

	/**
	 * The Id of the specific line 
	 */
	private int _parentId;
	/**
	 * 
	 */
	private String _nameStation;
	/**
	 * The list of station in the specific line
	 */
	private ArrayList<Station> _listStation;
	/**
	 * The view of the list of station
	 */
	private ListView _listViewStation;
	/**
	 * The attribute handling the connection to the database
	 */
	private StationDataSource _stationDataSource;
	/**
	 * Listener on the list view. Retrieve the Station selected and display the new Activity
	 */
	private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position, long id) {
			Intent intent = new Intent((SeveralStationActivity)getActivity(), StationActivity.class);
			intent.putExtra("idStation", _listStation.get(position).getStationId());
			intent.putExtra("lineId", _listStation.get(position).get_line_id());
			startActivity(intent);
		}
	};

	/**
	 * Default constructors
	 */
	public SeveralStationFragment(){
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		_stationDataSource = new StationDataSource(container.getContext());
		return inflater.inflate(R.layout.several_station_fragment, container, false);
	}

	@Override
	public void onStart() {
		super.onStart();

		TextView textView = (TextView) getView().findViewById(R.id.severalStationFragmentNameStation);
		textView.setText(_nameStation);

		_stationDataSource.open();
		_listStation = (ArrayList<Station>) _stationDataSource.getStationByParentId(_parentId); 
		_stationDataSource.close();

		_listViewStation = (ListView) getView().findViewById(R.id.severalStationFragmentListView);
		SeveralStationAdapter adapter = new SeveralStationAdapter(getActivity(), R.layout.several_station_row, _listStation);
		_listViewStation.setAdapter(adapter);

		_listViewStation.setOnItemClickListener(mMessageClickedHandler);
	}
	
	public void setName(String nameStation) {
		_nameStation = nameStation;
	}

	public void setParentId(int parentId) {
		_parentId = parentId;
	}

}
